/**
 * Copyright 2014  XCL-Charts
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @Project XCL-Charts
 * @Description Android图表基类库
 * @author XiongChuanLiang<br></br>(xcl_168@aliyun.com)
 * @license http://www.apache.org/licenses/  Apache v2 License
 * @version 1.0
 */
package com.gitee.wsl.common.chart.listener.click

import android.graphics.PointF
import org.xclcharts.common.MathHelper

/**
 * @ClassName ArcPosition
 * @Description  arc位置记录信息基类
 * @author XiongChuanLiang<br></br>(xcl_168@aliyun.com)
 */
open class ArcPosition : PositionRecord() {
    var offsetAngle = 0.0f
    var sweepAngle = 0.0f
        protected set
    var radius = 0.0f
        protected set
    var selectedOffset = 0.0f
        protected set

    //初始偏移角度
    var initAngle = 0.0f //180;

    var pointF: PointF? = null
        protected set

    //return mOffsetAngle;
    val angle: Float
        get() = MathHelper.getInstance().add(offsetAngle, sweepAngle)
    //return mOffsetAngle;
    /**
     * 饼图(pie chart)起始偏移角度
     * @param Angle 偏移角度
     */
    fun saveInitialAngle(Angle: Float) {
        initAngle = Angle
    }

    val startAngle: Float
        get() = MathHelper.getInstance().add(offsetAngle, initAngle)

    override fun compareRange(x: Float, y: Float): Boolean {
        // TODO Auto-generated method stub
        return if (null == pointF) false else compareRadius(x, y)
    }

    private fun compareRadius(x: Float, y: Float): Boolean {
        val distance = MathHelper.getInstance().getDistance(
            pointF!!.x, pointF!!.y, x, y
        )
        if (distance.compareTo(radius.toDouble()) == 0 || distance.compareTo(radius.toDouble()) == -1
        ) {
            val Angle1 = MathHelper.getInstance().getDegree(
                pointF!!.x, pointF!!.y, x, y
            ).toFloat()
            val currAngle = angle

            //??? 如果有设初始角度(mInitAngle)，则初始角度范围内的那个扇区点击判断会有问题，原因还没查出来			
            if (currAngle.compareTo(Angle1) == 1
                || currAngle.compareTo(Angle1) == 0
            ) {
                return true
            }
        }
        return false
    }
}